<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
function getDateTime()
{
    return date('Y-m-d H:i:s', time());
}
//生成订单号
function get_order_sn($prefix = 'OJ'){
    return $prefix . '-' .date('Ymd', time()) . '-' . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
}

function getRandStrByLength($length = 8)
{
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
    $password = '';
    for ($i = 0; $i < $length; $i++) {
        $password .= $chars[mt_rand(0, strlen($chars) - 1)];
    }
    return $password;
}

//更加可靠的判断一个变量在当前作用域中是否存在：
function __isset()
{
    if (array_key_exists('varShouldBeSet', get_defined_vars())) return true;
    return false;
}

/**
 * 简单对称加密算法之加密
 * @param String $string 需要加密的字串
 * @param String $skey 加密EKY
 * @author yfl <554665488@qq.com>
 * @date 2017年7月7日 11:46:35
 * @update 2017年7月7日 11:46:38
 * @return String
 */
function encode($string = '', $skey = 'oajiami')
{
    $strArr = str_split(base64_encode($string));
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key < $strCount && $strArr[$key] .= $value;
    return str_replace(array('=', '+', '/'), array('O0O0O', 'o000o', 'oo00o'), join('', $strArr));
}


/**
 * 简单对称加密算法之解密
 * @param String $string 需要解密的字串
 * @param String $skey 解密KEY
 * @author yfl <554665488@qq.com>
 * @date 2017年7月7日 11:46:26
 * @update 2017年7月7日 11:46:30
 * @return String
 */
function decode($string = '', $skey = 'oajiami')
{
    $strArr = str_split(str_replace(array('O0O0O', 'o000o', 'oo00o'), array('=', '+', '/'), $string), 2);
    $strCount = count($strArr);
    foreach (str_split($skey) as $key => $value)
        $key <= $strCount && isset($strArr[$key]) && $strArr[$key][1] === $value && $strArr[$key] = $strArr[$key][0];
    return base64_decode(join('', $strArr));
}

/**
 * @author: yfl
 * @QQ: 554665488
 * @description: 递归获取树状结构数组
 * @time: 2017-7-12 14:41:33
 * @param $array
 * @param int $type
 * @param int $pid
 * @param int $level
 * @return array
 */
function array_recursion($array, $type = 1, $pid = 0, $level = 1)
{
    if (!is_array($array)) return [];
    $result_data = [];
    if ($type == 1) {
        foreach ($array as $index => $item) {
            if ($item['upuid'] == $pid) {
                $item['level'] = $level;
                $result_data[$index] = $item;
                $result_data[$index]['child']=array_recursion($array,1,$item['uid'],$level+1);
            }
        }
    }
    return $result_data;
}

/**
 * 使用curl获取远程数据
 * @param  string $url url连接
 * @return string      获取到的数据
 * 自学php博客www.zixuephp.cn整理
 */
function curl_get_contents($url){
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);                //设置访问的url地址
    //curl_setopt($ch,CURLOPT_HEADER,1);                //是否显示头部信息
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);               //设置超时
    curl_setopt($ch, CURLOPT_USERAGENT, _USERAGENT_);   //用户访问代理 User-Agent
    curl_setopt($ch, CURLOPT_REFERER,_REFERER_);        //设置 referer
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);          //跟踪301
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);        //返回结果
    $r=curl_exec($ch);
    curl_close($ch);
    return $r;
}

/**
 * 不区分大小写的in_array()
 * @param  string $str   检测的字符
 * @param  array  $array 数组
 * @return boolear       是否in_array
 * 自学php博客www.zixuephp.cn整理
 */
function in_iarray($str,$array){
    $str=strtolower($str);
    $array=array_map('strtolower', $array);
    if (in_array($str, $array)) {
        return true;
    }
    return false;
}

/**
 * @param $base64_image_content
 * @param $path
 * @Author: yfl
 * @Email: 554665488@qq.com
 * @Date:二〇一九年八月十四日 22:46:14
 * @Description:将Base64图片转换为本地图片并保存
 * @return bool|string
 */
function base64_image_content($base64_image_content,$path){
    //匹配出图片的格式
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image_content, $result)){
        $type = $result[2];
        $new_file = $path."/".date('Ymd',time())."/";
        if(!file_exists($new_file)){
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir($new_file, 0700);
        }
        $new_file = $new_file.time().".{$type}";
        if (file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_image_content)))){
            return '/'.$new_file;
        }else{
            return false;
        }
    }else{
        return false;
    }
}

/**
 * @param $file
 * @return array
 * 解析excel 返回数据
 */
function import_excel($file){
    // 判断文件是什么格式
    $type = pathinfo($file);
    $type = strtolower($type["extension"]);
//    $type=$type==='csv' ? $type : 'Excel2007';
    $type=$type==='xls' ? 'Excel5' : 'Excel2007';
    ini_set('max_execution_time', '0');
    // 判断使用哪种格式
    $objReader = PHPExcel_IOFactory::createReader($type);
    $objPHPExcel = $objReader->load($file);
    $sheet = $objPHPExcel->getSheet(0);
    // 取得总行数
    $highestRow = $sheet->getHighestRow();
    // 取得总列数
    $highestColumn = $sheet->getHighestColumn();
    //循环读取excel文件,读取一条,插入一条
    $data=array();
    $excelData = $sheet->toArray();

    foreach ($excelData as  $index => $excelDatum) {
        if($index == 0) continue;
        $data[] = $excelDatum;
    }
    array_shift($data);
//    //从第一行开始读取数据
//    for($j=3;$j<=$highestRow;$j++){
//        //从A列读取数据
//        for($k='A';$k<=$highestColumn;$k++){
//            // 读取单元格
//            $data[$j][]=$objPHPExcel->getActiveSheet()->getCell("$k$j")->getValue();
//        }
//    }
    return $data;
}

/**
 * @return string 真实路径
 */
function getRealDir(){
    return dirname(__DIR__);
}


/**
 * 金额的小写转大写
 * @param $ns int 输入的数字
*/

function getChineseNumber($num)
{

    $c1 = "零壹贰叁肆伍陆柒捌玖";
    $c2 = "分角元拾佰仟万拾佰仟亿";
    $num = round($num, 2);
    $num = $num * 100;
    if (strlen($num) > 10) {
        return "数据太长，没有这么大的钱吧，检查下";
    }
    $i = 0;
    $c = "";
    while (1) {
        if ($i == 0) {
            $n = substr($num, strlen($num)-1, 1);
        } else {
            $n = $num % 10;
        }
        $p1 = substr($c1, 3 * $n, 3);
        $p2 = substr($c2, 3 * $i, 3);
        if ($n != '0' || ($n == '0' && ($p2 == '亿' || $p2 == '万' || $p2 == '元'))) {
            $c = $p1 . $p2 . $c;
        } else {
            $c = $p1 . $c;
        }
        $i = $i + 1;
        $num = $num / 10;
        $num = (int)$num;
        if ($num == 0) {
            break;
        }
    }
    $j = 0;
    $slen = strlen($c);
    while ($j < $slen) {
        $m = substr($c, $j, 6);
        if ($m == '零元' || $m == '零万' || $m == '零亿' || $m == '零零') {
            $left = substr($c, 0, $j);
            $right = substr($c, $j + 3);
            $c = $left . $right;
            $j = $j-3;
            $slen = $slen-3;
        }
        $j = $j + 3;
    }

    if (substr($c, strlen($c)-3, 3) == '零') {
        $c = substr($c, 0, strlen($c)-3);
    }
    if (empty($c)) {
        return "零元整";
    }else{
        return $c . "整";
    }

}